package com.event.management.frontend.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.event.management.frontend.common.Result;
import com.event.management.frontend.dto.UserTicketDTO;
import com.event.management.frontend.service.UserTicketService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户电子票控制器
 */
@Api(tags = "用户电子票接口")
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/front")
public class UserTicketController {

    private final UserTicketService userTicketService;

    /**
     * 获取用户电子票列表 - 需要登录
     */
    @ApiOperation("获取用户电子票列表")
    @GetMapping("/tickets")
    @SaCheckLogin
    public Result<Result.Page<UserTicketDTO>> getUserTickets(
            @ApiParam("页码") @RequestParam(defaultValue = "1") int page,
            @ApiParam("每页大小") @RequestParam(defaultValue = "10") int size) {
        // 从当前用户上下文获取userId
        Long userId = Long.valueOf(StpUtil.getLoginIdAsString());
        
        Result.Page<UserTicketDTO> result = new Result.Page<>();
        result.setRecords(userTicketService.getUserTickets(userId, page, size));
        result.setTotal(userTicketService.countUserTickets(userId));
        return Result.ok(result);
    }

    /**
     * 获取电子票详情 - 需要登录
     */
    @ApiOperation("获取电子票详情")
    @GetMapping("/tickets/{ticketId}")
    @SaCheckLogin
    public Result<UserTicketDTO> getTicketDetail(
            @ApiParam("电子票ID") @PathVariable Long ticketId) {
        // 从当前用户上下文获取userId
        Long userId = Long.valueOf(StpUtil.getLoginIdAsString());
        
        UserTicketDTO ticket = userTicketService.getTicketDetail(userId, ticketId);
        return Result.ok(ticket);
    }

    /**
     * 获取订单相关的电子票列表 - 无需登录，通过订单ID查询
     */
    @ApiOperation("获取订单相关的电子票列表")
    @GetMapping("/orders/{orderId}/tickets")
    public Result<List<UserTicketDTO>> getTicketsByOrderId(
            @ApiParam("订单ID") @PathVariable Long orderId) {
        List<UserTicketDTO> tickets = userTicketService.getTicketsByOrderId(orderId);
        return Result.ok(tickets);
    }
    
    /**
     * 用户标记电子票为已使用 - 需要登录
     */
    @ApiOperation("用户标记电子票为已使用")
    @PostMapping("/tickets/{ticketId}/use")
    @SaCheckLogin
    public Result<Boolean> markTicketAsUsed(
            @ApiParam("电子票ID") @PathVariable Long ticketId) {
        // 从当前用户上下文获取userId
        Long userId = Long.valueOf(StpUtil.getLoginIdAsString());
        
        boolean success = userTicketService.markTicketAsUsed(userId, ticketId);
        if (success) {
            return Result.ok(true, "标记电子票为已使用成功");
        } else {
            return Result.fail("标记电子票为已使用失败");
        }
    }

    /**
     * 验票接口 - 管理员使用
     */
    @ApiOperation("验票接口")
    @PostMapping("/admin/tickets/check")
    public Result<Boolean> checkTicket(
            @ApiParam("票码") @RequestParam String ticketCode) {
        boolean success = userTicketService.checkTicket(ticketCode);
        return Result.ok(success);
    }
} 